//
//  EventDisposeVC.m
//  ConvenientlyTake
//
//  Created by 森鑫 on 2022/12/23.
//

#import "EventDisposeVC.h"
#import "EvenDetailBottomView.h"
#import "EventDetailAPIRequest.h"
#import "EventDetailModel.h"

@interface EventDisposeVC ()<TGChoosePhotoViewDelegate, QMUITextViewDelegate, QMUITextFieldDelegate,EvenDetailBottomViewBtnDelegate>
@property (weak, nonatomic) IBOutlet QMUITextView *descTextView;
@property (weak, nonatomic) IBOutlet UILabel *descNumLabel;


@property (weak, nonatomic) IBOutlet UILabel *photoNumLabel;
@property (weak, nonatomic) IBOutlet TGChoosePhotoView *choosePhotoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *choosePhotoViewHeight;

@property (nonatomic, strong) NSArray *imgUrlInfoArray;
@property (nonatomic , strong) EvenDetailBottomView *bottomView;

@end

@implementation EventDisposeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"处置";
    [self setupUI];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self setNavStyleWhiteAndBackBtn];
}

- (void)setupUI {
    self.descTextView.placeholder = @"请在此输入处置描述";
    self.descTextView.placeholderColor = TextViewPlaceholderColor;
    self.descTextView.font = TGMediumAFont(15);
    self.descTextView.textColor = DeepBlackColor;
    self.descTextView.delegate = self;
    
    self.choosePhotoView.uploadImageType = UploadImageType_CONTRACT_SERVICE;
    self.choosePhotoView.maxCount = 10;
    self.choosePhotoViewHeight.constant = PhotoCollectionViewHeight(1);
    self.choosePhotoView.delegate = self;
    
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(102);
    }];
    if (self.detailsModel){
        self.descTextView.text = self.detailsModel.disposalDesc;
        NSMutableArray *serviceFileImgInfoArray = [NSMutableArray arrayWithCapacity:self.detailsModel.disposePictureList.count];
        for (DisposeFileList *fileModel in self.detailsModel.disposePictureList) {
            TGUrlInfoModel *imgModel = [TGUrlInfoModel urlInfoModelWithFileName:fileModel.fileName url:fileModel.filePath];
            [serviceFileImgInfoArray addObject:imgModel];
        }
        self.choosePhotoView.selectImageUrlInfoArray = serviceFileImgInfoArray;
        [self tgChoosePhotoViewDidSelectImgArray:@[] imgUrlInfoArray:serviceFileImgInfoArray];
    }
    
}

#pragma mark bottomViewDelegate
- (void)selelctItemWitIndex:(NSInteger)index andSender:(UIButton *)sender{
    NSMutableArray *imagesList = [NSMutableArray array];
    for (TGUrlInfoModel *imgInfoModel in self.imgUrlInfoArray) {
        [imagesList addObject:@{@"fileType": @"1", @"fileName": R(imgInfoModel.filename), @"filePath": R(imgInfoModel.fullPath)}];
    }
    if([sender.titleLabel.text isEqualToString:@"保存"]) {
        [TGProgressHUD showHUD:nil];
        [EventDetailAPIRequest POSTWorkingDisposalSaveWithEventId:self.eventId disposalDesc:self.descTextView.text imagesList:imagesList success:^(TGResponseItem *responseItem) {
            [TGProgressHUD dismissMPHUD];
            if (responseItem.success) {
                [TGProgressHUD showHUDTip:@"保存成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventDetailVC object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventCenterVC object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }
        } fail:^(NSError *error) {
            [TGProgressHUD dismissMPHUD];
        }];
    }
    if ([sender.titleLabel.text isEqualToString:@"提交"]) {
        [TGProgressHUD showHUD:nil];
        [EventDetailAPIRequest POSTWorkingDisposalSubmmitWithEventId:self.eventId disposalDesc:self.descTextView.text imagesList:imagesList success:^(TGResponseItem *responseItem) {
            [TGProgressHUD dismissMPHUD];
            if (responseItem.success) {
                [TGLEEAlert eventAlertTitle:@"提交成功" message:@"处置结果已提报，请等待上级审核，向您表示感谢！" cancelTitle:nil sureTitle:@"返回" clickBackgroundClose:YES click:^(NSInteger index) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventDetailVC object:nil];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationWithRefresheEventCenterVC object:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }
        } fail:^(NSError *error) {
            [TGProgressHUD dismissMPHUD];
        }];
    }
}


#pragma mark QMUITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 200) {
        [TGProgressHUD showHUDTip:@"处置描述不能超过200个字符"];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:@"200/200"];
        [attStr addAttribute:NSForegroundColorAttributeName value:UIColorRed range:NSMakeRange(0, 3)];
        self.descNumLabel.attributedText = attStr;
        self.descTextView.text = [textView.text substringToIndex:200];
    } else {
        self.descNumLabel.text = [NSString stringWithFormat:@"%zd/200", textView.text.length];
    }
}


#pragma mark TGChoosePhotoViewDelegate
- (void)tgChoosePhotoViewDidSelectImgArray:(NSArray *)imgArray imgUrlInfoArray:(NSArray <TGUrlInfoModel *>*)imgUrlInfoArray {
    NSLog(@"++++%@,-----%@", imgArray, imgUrlInfoArray);
    self.photoNumLabel.text = [NSString stringWithFormat:@"照片(%lu/10)" , (unsigned long)imgArray.count];
    NSInteger count = ceil((imgArray.count+1)/4.0);
    self.choosePhotoViewHeight.constant = PhotoCollectionViewHeight(count);
    self.imgUrlInfoArray = imgUrlInfoArray;
}

- (EvenDetailBottomView *)bottomView{
    if(!_bottomView){
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"EvenDetailBottomView" owner:self options:0] firstObject];
        _bottomView.delegate = self;
        [_bottomView.leftBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_bottomView.rightBtn setTitle:@"提交" forState:UIControlStateNormal];

    }
    return _bottomView;
}

@end

